package cn.tedu.weibo.controller;

import cn.tedu.weibo.mappers.CommentMapper;
import cn.tedu.weibo.pojo.DTO.CommentDTO;
import cn.tedu.weibo.pojo.VO.CommentVO;
import cn.tedu.weibo.pojo.VO.UserVO;
import cn.tedu.weibo.pojo.entity.Comment;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment/")
@Api(tags = "3.评论模块")
public class CommentController {
    @Autowired(required = false)
    private CommentMapper commentMapper;

    /**
     * 发布评论功能
     * @param commentDTO
     * @param session
     * @return
     */
    @GetMapping("insert")
    public int insert(@RequestBody CommentDTO commentDTO, HttpSession session){
        System.out.println("commentDTO"+commentDTO);
        UserVO userVO=(UserVO)session.getAttribute("user");
        if (userVO==null){
            return 2;
        }
        Comment comment=new Comment();
        comment.setContent(commentDTO.getContent());
        comment.setCreated(new Date());
        comment.setUser_id(userVO.getId());
        comment.setWeibo_id(commentDTO.getWeiboId());
        commentMapper.insert(comment);
        return 1;
    }
    /**
     * 评论列表
     * @param id
     * @return
     */
    @GetMapping("selectByWeiboId")
    public List<CommentVO> selectByWeiboId(int id){
        return commentMapper.selectByWeiboId(id);
    }


}
